const formatTime = (date) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day]
    .map(formatNumber)
    .join('/')} ${[hour, minute, second]
    .map(formatNumber)
    .join(':')}`
}

/**
 * 时间戳
 * @param {*} timestamp  时间戳
 * @param {*} format  格式
 */
var timestampToTime = function (
  timestamp,
  format
) {
  if (
    timestamp.toString().indexOf('Date') != -1
  ) {
    timestamp = parseInt(
      timestamp
      .toString()
      .replace('/Date(', '')
      .replace(')/', ''),
      10
    )
  }
  let date = new Date(timestamp) //时间戳为10位需*1000，时间戳为13位的话不需乘1000
  // console.log(date)
  let Y = date.getFullYear()
  let M =
    date.getMonth() + 1 < 10 ?
    '0' + (date.getMonth() + 1) :
    date.getMonth() + 1
  let D =
    date.getDate() < 10 ?
    '0' + date.getDate() :
    date.getDate()
  let h =
    (date.getHours() < 10 ?
      '0' + date.getHours() :
      date.getHours()) + ':'
  let H =
    date.getHours() < 10 ?
    '0' + date.getHours() :
    date.getHours()
  let m =
    (date.getMinutes() < 10 ?
      '0' + date.getMinutes() :
      date.getMinutes()) + ':'
  let mT =
    date.getMinutes() < 10 ?
    '0' + date.getMinutes() :
    date.getMinutes()
  let s =
    date.getSeconds() < 10 ?
    '0' + date.getSeconds() :
    date.getSeconds()
  let str = ''
  switch (format) {
    case 'y':
      str = Y
      break
    case 'm':
      str = M
      break
    case 'd':
      str = D
      break
    case 'y+m':
      str = Y + '-' + M
      break
    case 'y + m':
      str = Y + '-' + M
      break
    case 'y+m+d':
      str = Y + '-' + M + '-' + D
      break
    case 'y + m + d':
      str = Y + '-' + M + '-' + D
      break
    case 'm+d':
      str = M + '-' + D
      break
    case 'm + d':
      str = M + '-' + D
      break
    case 'y + m + d + h + f + s':
      str =
        Y + '-' + M + '-' + D + ' ' + h + m + s
      break
    case 'y+m+d+h+f+s':
      str =
        Y + '-' + M + '-' + D + ' ' + h + m + s
      break
    case 'y + m + d + h + f':
      str = Y + '-' + M + '-' + D + ' ' + h + m
      break
    case 'y+m+d+h+f':
      str = Y + '-' + M + '-' + D + ' ' + h + mT
      break
    case 'm + d + h + f':
      str = M + '-' + D + ' ' + h + mT
      break
    case 'm + d + h + f + str':
      str =
        M +
        '月' +
        D +
        '日' +
        ' ' +
        H +
        '时' +
        mT +
        '分'
      break
    case 'm+d+h+f':
      str = M + '-' + D + ' ' + h + mT
      break
    case 'm+d+h+f+str':
      str =
        M +
        '月' +
        D +
        '日' +
        ' ' +
        H +
        '时' +
        mT +
        '分'
      break
    case 'h+m':
      str = h + m
      break
    case 'h + m':
      str = h + m
      break
    default:
      str =
        Y + '-' + M + '-' + D + ' ' + h + m + s
      break
  }
  return str
}

const formatNumber = (n) => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

module.exports = {
  formatTime,
  timestampToTime
}